define(["jquery"],function($){
	var quest = {};
	
	/**
	 * 数据处理(若不需要数据处理，可以不写)
	 */
    quest.formatData = function(obj){
    	if( !obj.list ){
	    	obj.list = [];
	    	var province = {
	    		"id":obj.id + "_province",
	    		"innDeCode":"HDSB01.02.009",
	            "deCode":"DE02.01.009.01",
	            "subunit":"省",
	            "type":"select",		
	    	};
	    	var city = {
	    		"id":obj.id + "_city",
	    		"innDeCode":"HDSB01.02.010",
	            "deCode":"DE02.01.009.02",
	            "subunit":"市",
	            "type":"select",
	    	};
	    	var county = {
	    		"id":obj.id + "_county",
	    		"innDeCode":"HDSB01.02.011",
	            "deCode":"DE02.01.009.03",
	            "subunit":"区/县",
	            "type":"select",
	    	};
	    	var street = {
	    		"id":obj.id + "_street",
	    		"innDeCode":"HDSB01.02.012",
	            "deCode":"DE02.01.009.04",
	            "placeHolder":"街道/乡/镇",
	            "type":"input",
	    	};
	    	var community = {
	    		"id":obj.id + "_community",
	    		"innDeCode":"HDSB01.02.013",
	            "deCode":"DE02.01.009.05",
	            "placeHolder":"小区/村",
	            "type":"input",
	    	};
	    	var room = {
	    		"id":obj.id + "_room",
	    		"innDeCode":"HDSB01.02.014",
	            "deCode":"DE02.01.009.06",
	            "placeHolder":"门牌号",
	            "type":"input",
	    	};
	    	obj.list.push(province);
	    	obj.list.push(city);
	    	obj.list.push(county);
	    	obj.list.push(street);
	    	obj.list.push(community);
	    	obj.list.push(room);
    	}
    	var len = obj.list.length;
    	obj.width = (100 / len) + "%";
    	for (var i = 0; i < len; i++) {
    		if( obj.list[i].deCode ) obj.list[i].name = obj.list[i].deCode.replace(/\./g, "_"); // “.”不能作为name的名称
    		if( obj.list[i].subunit ){
    			obj.list[i].textWidth = Number(obj.list[i].subunit.length) + Number(1) + 'em';
    		}else{
    			obj.list[i].textWidth = "0.5em";
    		}
    	}
		return obj;
	}

	/**
	 * tpl模板
	 */
	quest.tpl = function(){
		return 	'<script id="tpl_question_address" type="text/html">'+
				'	<div {{if relationId}}relationId="{{relationId}}"{{/if}} class="qaCard qaCard-address form-group form-group-sm col-sm-{{length}} col-md-{{length}} grid-label-{{labelLength}}">'+
				'		<label class="control-label {{align}}">{{label}}</label>'+
				'   	<div class="control-input qaCard-multiSelect clearfix">'+
				'			{{if status=="default"}}'+
				'				{{each list as listobj i}}'+
				'					<div class="multiple" style="width:{{width}}">'+
				'						{{if listobj.type=="select"}}'+
				'							<select class="form-control validatebox" style="width:calc(100% - {{listobj.textWidth}});" name="{{listobj.name}}" data-type="{{type}}" data-quesId="{{listobj.id}}" data-innDeCode="{{listobj.innDeCode}}" data-deCode="{{listobj.deCode}}" data-validate="{{validate}}" data-attention="{{attention}}"></select>'+
				'						{{else listobj.type=="input"}}'+
				'							<input type="text" class="form-control validatebox" style="width:calc(100% - {{listobj.textWidth}});" name="{{listobj.name}}" data-type="{{type}}" data-quesId="{{listobj.id}}" data-innDeCode="{{listobj.innDeCode}}" data-deCode="{{listobj.deCode}}" data-validate="{{validate}}" data-attention="{{attention}}" placeholder="{{listobj.placeHolder}}">'+
				'						{{/if}}'+
				'						<span style="width:{{listobj.textWidth}}">{{listobj.subunit}}</span>'+
				'					</div>'+
				'				{{/each}}'+
				'			{{else if status=="readonly"}}'+
				'				{{each list as listobj i}}'+
				'					<div class="multiple" style="width:{{width}}">'+
				'						<p class="form-control-static" style="width:calc(100% - {{listobj.textWidth}});" name="{{listobj.name}}" data-type="{{type}}" data-quesId="{{listobj.id}}" data-innDeCode="{{listobj.innDeCode}}" data-deCode="{{listobj.deCode}}" data-validate="{{validate}}" data-attention="{{attention}}"></p>'+
				'						<span style="width:{{listobj.textWidth}}">{{listobj.subunit}}</span>'+
				'					</div>'+
				'				{{/each}}'+
				'			{{else if status=="disabled"}}'+
				'				{{each list as listobj i}}'+
				'					<div class="multiple" style="width:{{width}}">'+
				'						{{if listobj.type=="select"}}'+
				'							<select class="form-control validatebox" style="width:calc(100% - {{listobj.textWidth}});" name="{{listobj.name}}" data-type="{{type}}" data-quesId="{{listobj.id}}" data-innDeCode="{{listobj.innDeCode}}" data-deCode="{{listobj.deCode}}" data-validate="{{validate}}" data-attention="{{attention}} disabled"></select>'+
				'						{{else listobj.type=="input"}}'+
				'							<input type="text" class="form-control validatebox" style="width:calc(100% - {{listobj.textWidth}});" name="{{listobj.name}}" data-type="{{type}}" data-quesId="{{listobj.id}}" data-innDeCode="{{listobj.innDeCode}}" data-deCode="{{listobj.deCode}}" data-validate="{{validate}}" data-attention="{{attention}}" placeholder="{{listobj.placeHolder}}" disabled>'+
				'						{{/if}}'+
				'						<span style="width:{{listobj.textWidth}}">{{listobj.subunit}}</span>'+
				'					</div>'+
				'				{{/each}}'+
				'			{{/if}}'+
				'  		</div>'+
				'	</div>'+
				'</script>';
	}

	/**
	 * 赋值
	 */
	quest.setValue = function($dom,value){
		if( value ){
			if( $dom.hasClass("form-control-static") ){
				$dom.html(value.quesValue);
			}else{
				var index = $dom.parent().index();
				if( index == 0 ){
					setTimeout(function(){
						$dom.val(value.quesValue);
						$dom.trigger("change");
					},500);
				}else if( index == 1 ){
					setTimeout(function(){
						$dom.val(value.quesValue);
						$dom.trigger("change");
					},500);
				}else if( index == 2 ){
					setTimeout(function(){
						$dom.val(value.quesValue);
					},500);
				}else if( index == 3 || index == 4 || index == 5 ){
					$dom.val(value.quesValue);
				}
			}
		}
	}

	/**
	 * 获取value（保存时，获取每个dom对应的答案）
	 */
	quest.getValue = function($dom,name){
		var val = $dom.hasClass("form-control-static") ? $dom.html() : $dom.val();
		var value = {};
		value.quesValue = val;
		value.quesId = $dom.attr("data-quesId");
		value.deCode = $dom.attr("data-deCode");
		value.innDeCode = $dom.attr("data-innDeCode");
		return value;
	}

	/**
	 * 事件(若没有事件函数，可以不写)
	 */
	quest.event = function(params,id){
		$("#" + id + " .qaCard-address").each(function(i){
			var $select = $(this).find(".control-input > div > select");
			var $province = $select.eq(0), $city = $select.eq(1), $county = $select.eq(2);
			$.getJSON('scprit/followup/data/position/position.json',function(data){
				var province = data.province, city = data.city, county = data.county;
				var h1 = '<option value="" selfId="0">请选择</option>';
				for (var i = 0; i < province.length; i++) {
					h1 += '<option value="'+province[i].self_name+'" selfId="'+province[i].self_id+'">'+ province[i].self_name +'</option>';
				}
				$province.html(h1);
				$province.data({"value":{"data":city,"$dom":$city}})
				$province.bind("change",onChangeSelect);
				$city.data({"value":{"data":county,"$dom":$county}})
				$city.bind("change",onChangeSelect);
	        });
		});
	}

	/**
	 * 省市区三级联动select变化时相关联
	 */
	var onChangeSelect = function(){
		var options = $(this).data("value");
		var data = options.data, $dom = options.$dom;
		var selfId = $(this).find("option:selected").attr("selfId");
		var h = '<option value="" selfId="0">请选择</option>';
		if ( selfId > 0 ) {
			for (var i = 0; i < data.length; i++) {
				if (data[i].parent_id == selfId ){
					h += '<option value="'+data[i].self_name+'" selfId="'+data[i].self_id+'">'+ data[i].self_name +'</option>';
				}
			}
		}
		$dom.html(h);
	}

	return quest;
});